//
//  LJMUserCollectionViewCell.m
//  wind
//
//  Created by 李金明 on 16/1/11.
//  Copyright © 2016年 李金明. All rights reserved.
//

#import "LJMUserCollectionViewCell.h"
#import "DYZFourTableViewCell.h"
#import "DYZFavouriteTableViewCell.h"
#import "DYZSongListTableViewCell.h"
#import "DYZSongListHeaderView.h"
#import "DYZCollectedTableViewCell.h"

@interface LJMUserCollectionViewCell ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *tableV;

#pragma 头视图内容_SYP

@property (nonatomic, retain) UITapGestureRecognizer *tapGR;

@end

@implementation LJMUserCollectionViewCell

- (void)dealloc
{
    Block_release(myBlock);
    [_tapGR release];
    [_headerImageV release];
    [_tableV release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatView];
    }
    return self;
}
- (void)creatView
{
    //添加tableView
    [self addTableView];
}

#pragma 添加tableView
- (void)addTableView
{
    
    self.tableV = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    
    // 实现滑动缩放_SYP
    self.tableV.contentInset = UIEdgeInsetsMake(SCREEN_HEIGHT / 2.5, 0, 0, 0);

    self.tableV.showsVerticalScrollIndicator = NO;
    
    NSString *imageFile = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents" ] stringByAppendingPathComponent:@"myImage"];
    UIImage *image = [UIImage imageWithContentsOfFile:imageFile];
        self.headerImageV = [[UIImageView alloc] init];
    
    if (nil != image) {
        self.headerImageV.image = image;
    } else {
        self.headerImageV.image = [UIImage imageNamed:@"壁纸2"];
    }

    self.headerImageV.frame = CGRectMake(0, -SCREEN_HEIGHT / 3, SCREEN_WIDTH, SCREEN_HEIGHT / 3);
    self.headerImageV.userInteractionEnabled = YES;
    self.headerImageV.contentMode = UIViewContentModeScaleAspectFill;
    [self.tableV addSubview: self.headerImageV];
    [self.headerImageV release];
    
    self.tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRAction)];
    [self.headerImageV addGestureRecognizer:self.tapGR];
    [_tapGR release];
    
    
    
    
    
    
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:self.tableV];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"滑动了");
    CGFloat y = scrollView.contentOffset.y;
    NSLog(@"大小:%f", y);
    if (y < - (SCREEN_HEIGHT / 3)) {
        CGRect frame = _headerImageV.frame;
        frame.origin.y = y;
        frame.size.height = -y;
        _headerImageV.frame = frame;
    }
}

#pragma 点击顶部实现更换壁纸界面
- (void)tapGRAction
{
    NSLog(@"更换壁纸");
    myBlock();
}

- (void)sendsBlock:(blockTypes)block
{
    myBlock = Block_copy(block);
}
#pragma 行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.section) {
//        return self.frame.size.height / 4;
        return 0;
    } else if (1 == indexPath.section) {
        return self.frame.size.height / 7;
    } else {
        return self.frame.size.height / 7;
    }
}

#pragma 添加tableView的头视图_SYP
- (UIView *)creatTableViewHeaderView
{
    UIView *headerV = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 3)] autorelease];
    headerV.backgroundColor = [UIColor colorWithRed:0.653 green:0.290 blue:1.000 alpha:1.000];
    
    self.headerImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"壁纸2"]];
    self.headerImageV.frame = headerV.frame;
    [headerV addSubview:self.headerImageV];
    [_headerImageV release];
    
    return headerV;
    
}

#pragma 实现下拉缩放


// 区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
//    if (2 == section) {
//        return self.frame.size.height / 16;
//    } else {
//        return 0;
//    }
    return 0;
}

// 区头视图
//- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    if (2 == section) {
//        DYZSongListHeaderView *songListHeaderV = [[DYZSongListHeaderView alloc] initWithFrame:CGRectZero];
//        return songListHeaderV;
//    } else {
//        return nil;
//    }
//}

// 区标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (1 == section) {
        return @"我的收藏";
    } else {
        return nil;
    }
}

// 区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (0 == section) {
        return 0;
    }
    if (1 == section) {
        return 2;
    } else {
        return 0; // 按数据数量改变
    }
}
// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.section) {
        static NSString *fourCell = @"four";
        DYZFourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:fourCell];
        if (cell == nil) {
            cell = [[[DYZFourTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:fourCell] autorelease];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (1 == indexPath.section) {
        if (0 == indexPath.row) {
            static NSString *favouriteCell = @"favourite";
            DYZFavouriteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:favouriteCell];
            if (cell == nil) {
                cell = [[[DYZFavouriteTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:favouriteCell] autorelease];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        } else {
            static NSString *collectedCell = @"collected";
            DYZCollectedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:collectedCell];
            if (cell == nil) {
                cell = [[[DYZCollectedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:collectedCell] autorelease];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
       
    } else {
        static NSString *songListCell = @"songList";
        DYZSongListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:songListCell];
        if (cell == nil) {
            cell = [[[DYZSongListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:songListCell] autorelease];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }

}

@end
